package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.mapper.MessageMapper;
import cn.wolfcode.trip.base.query.MessageQueryObject;
import cn.wolfcode.trip.base.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MessageServiceImpl implements IMessageService{
    @Autowired
    private MessageMapper messageMapper;

    @Override
    public List<Map> selectChartPerson(Long id) {
        return messageMapper.selectChartPerson(id);
    }

    @Override
    public List<Message> selectChartMessages(MessageQueryObject qo) {
        List<Message> ms = messageMapper.selectChartMessages(qo);
        for (Message m : ms) {
            if (m.getState()==false) {
                messageMapper.updateState(m.getId(),true);
            }
        }
        return ms;
    }
    @Override
    public List<Message> selectNewChartMessages(MessageQueryObject qo) {
        List<Message> ms = messageMapper.selectNewChartMessages(qo);
        for (Message m : ms) {
            if (m.getState()==false) {
                messageMapper.updateState(m.getId(),true);
            }
        }
        return ms;
    }

    @Override
    public List<Message> saveMessage(Message message) {
        //创建聊天时间,并向前台返回存储信息,用于回显
        Date currentTime = new Date();
        message.setCurrentTime(currentTime);
        messageMapper.saveMessage(message);
        Message m = messageMapper.selectByPrimaryKey(message.getId());
        List<Message> list = new ArrayList<>();
        list.add(m);
        return list;
    }

    @Override
    public int selectMessageCount(Long receiverId) {
        return messageMapper.selectMessageCount(receiverId);
    }

    @Override
    public List<Map> selectChartCount(Long receiverId) {
        return messageMapper.selectChartCount(receiverId);
    }

}
